Imports System.Xml
Imports System.Net.Mail
Imports System.IO
Imports System.Text

Public Class Form1
	Inherits System.Windows.Forms.Form

	Protected m_clearque As New ClearingQueue
	Protected m_lastDate As New Date
	Protected m_proced As Long
	Friend WithEvents wbSent As System.Windows.Forms.WebBrowser
	Friend WithEvents Button1 As System.Windows.Forms.Button
	Friend WithEvents wbReply As System.Windows.Forms.WebBrowser
	Friend WithEvents Button2 As System.Windows.Forms.Button
	Friend WithEvents Button3 As System.Windows.Forms.Button
	Friend WithEvents Button4 As System.Windows.Forms.Button
	Friend WithEvents Button5 As System.Windows.Forms.Button
	Friend WithEvents Button6 As System.Windows.Forms.Button
	Friend WithEvents Button7 As System.Windows.Forms.Button
	Friend WithEvents Button8 As System.Windows.Forms.Button
	Friend WithEvents Button9 As System.Windows.Forms.Button
	Friend WithEvents Button11 As System.Windows.Forms.Button
	Friend WithEvents Button10 As System.Windows.Forms.Button
	Protected m_username As String

#Region " Windows Form Designer generated code "

	Public Sub New()
		MyBase.New()

		'This call is required by the Windows Form Designer.
		InitializeComponent()

		'Add any initialization after the InitializeComponent() call

	End Sub

	'Form overrides dispose to clean up the component list.
	Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
		If disposing Then
			If Not (components Is Nothing) Then
				components.Dispose()
			End If
		End If
		MyBase.Dispose(disposing)
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	Friend WithEvents QueueTimer As System.Timers.Timer
	Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
	Friend WithEvents btToggleTimer As System.Windows.Forms.Button
	Friend WithEvents Label1 As System.Windows.Forms.Label
	Friend WithEvents Label2 As System.Windows.Forms.Label
	Friend WithEvents lbLastProcessed As System.Windows.Forms.Label
	Friend WithEvents lbNoProcessed As System.Windows.Forms.Label
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.QueueTimer = New System.Timers.Timer
		Me.Splitter1 = New System.Windows.Forms.Splitter
		Me.btToggleTimer = New System.Windows.Forms.Button
		Me.Label1 = New System.Windows.Forms.Label
		Me.Label2 = New System.Windows.Forms.Label
		Me.lbLastProcessed = New System.Windows.Forms.Label
		Me.lbNoProcessed = New System.Windows.Forms.Label
		Me.wbSent = New System.Windows.Forms.WebBrowser
		Me.Button1 = New System.Windows.Forms.Button
		Me.wbReply = New System.Windows.Forms.WebBrowser
		Me.Button2 = New System.Windows.Forms.Button
		Me.Button3 = New System.Windows.Forms.Button
		Me.Button4 = New System.Windows.Forms.Button
		Me.Button5 = New System.Windows.Forms.Button
		Me.Button6 = New System.Windows.Forms.Button
		Me.Button7 = New System.Windows.Forms.Button
		Me.Button8 = New System.Windows.Forms.Button
		Me.Button9 = New System.Windows.Forms.Button
		Me.Button10 = New System.Windows.Forms.Button
		Me.Button11 = New System.Windows.Forms.Button
		CType(Me.QueueTimer, System.ComponentModel.ISupportInitialize).BeginInit()
		Me.SuspendLayout()
		'
		'QueueTimer
		'
		Me.QueueTimer.Interval = 10000
		Me.QueueTimer.SynchronizingObject = Me
		'
		'Splitter1
		'
		Me.Splitter1.Location = New System.Drawing.Point(0, 0)
		Me.Splitter1.Name = "Splitter1"
		Me.Splitter1.Size = New System.Drawing.Size(3, 601)
		Me.Splitter1.TabIndex = 1
		Me.Splitter1.TabStop = False
		'
		'btToggleTimer
		'
		Me.btToggleTimer.Location = New System.Drawing.Point(196, 4)
		Me.btToggleTimer.Name = "btToggleTimer"
		Me.btToggleTimer.Size = New System.Drawing.Size(128, 23)
		Me.btToggleTimer.TabIndex = 2
		Me.btToggleTimer.Text = "Start Timer"
		'
		'Label1
		'
		Me.Label1.Location = New System.Drawing.Point(7, 9)
		Me.Label1.Name = "Label1"
		Me.Label1.Size = New System.Drawing.Size(88, 16)
		Me.Label1.TabIndex = 3
		Me.Label1.Text = "Last Processed:"
		'
		'Label2
		'
		Me.Label2.Location = New System.Drawing.Point(7, 32)
		Me.Label2.Name = "Label2"
		Me.Label2.Size = New System.Drawing.Size(145, 23)
		Me.Label2.TabIndex = 4
		Me.Label2.Text = "Messages Processed Today:"
		'
		'lbLastProcessed
		'
		Me.lbLastProcessed.Location = New System.Drawing.Point(90, 9)
		Me.lbLastProcessed.Name = "lbLastProcessed"
		Me.lbLastProcessed.Size = New System.Drawing.Size(100, 23)
		Me.lbLastProcessed.TabIndex = 5
		Me.lbLastProcessed.Text = "None"
		'
		'lbNoProcessed
		'
		Me.lbNoProcessed.Location = New System.Drawing.Point(158, 32)
		Me.lbNoProcessed.Name = "lbNoProcessed"
		Me.lbNoProcessed.Size = New System.Drawing.Size(24, 23)
		Me.lbNoProcessed.TabIndex = 6
		Me.lbNoProcessed.Text = "0"
		'
		'wbSent
		'
		Me.wbSent.Location = New System.Drawing.Point(0, 81)
		Me.wbSent.MinimumSize = New System.Drawing.Size(20, 20)
		Me.wbSent.Name = "wbSent"
		Me.wbSent.Size = New System.Drawing.Size(519, 520)
		Me.wbSent.TabIndex = 7
		'
		'Button1
		'
		Me.Button1.Location = New System.Drawing.Point(249, 41)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(75, 23)
		Me.Button1.TabIndex = 8
		Me.Button1.Text = "Button1"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'wbReply
		'
		Me.wbReply.Location = New System.Drawing.Point(525, 81)
		Me.wbReply.MinimumSize = New System.Drawing.Size(20, 20)
		Me.wbReply.Name = "wbReply"
		Me.wbReply.Size = New System.Drawing.Size(535, 520)
		Me.wbReply.TabIndex = 9
		'
		'Button2
		'
		Me.Button2.Location = New System.Drawing.Point(341, 4)
		Me.Button2.Name = "Button2"
		Me.Button2.Size = New System.Drawing.Size(75, 23)
		Me.Button2.TabIndex = 10
		Me.Button2.Text = "Password"
		Me.Button2.UseVisualStyleBackColor = True
		Me.Button2.Visible = False
		'
		'Button3
		'
		Me.Button3.Location = New System.Drawing.Point(341, 41)
		Me.Button3.Name = "Button3"
		Me.Button3.Size = New System.Drawing.Size(75, 23)
		Me.Button3.TabIndex = 11
		Me.Button3.Text = "All Products"
		Me.Button3.UseVisualStyleBackColor = True
		'
		'Button4
		'
		Me.Button4.Location = New System.Drawing.Point(444, 4)
		Me.Button4.Name = "Button4"
		Me.Button4.Size = New System.Drawing.Size(75, 23)
		Me.Button4.TabIndex = 12
		Me.Button4.Text = "CL (FUT)"
		Me.Button4.UseVisualStyleBackColor = True
		Me.Button4.Visible = False
		'
		'Button5
		'
		Me.Button5.Location = New System.Drawing.Point(617, 4)
		Me.Button5.Name = "Button5"
		Me.Button5.Size = New System.Drawing.Size(75, 23)
		Me.Button5.TabIndex = 13
		Me.Button5.Text = "LO (OOC)"
		Me.Button5.UseVisualStyleBackColor = True
		Me.Button5.Visible = False
		'
		'Button6
		'
		Me.Button6.Location = New System.Drawing.Point(525, 4)
		Me.Button6.Name = "Button6"
		Me.Button6.Size = New System.Drawing.Size(83, 23)
		Me.Button6.TabIndex = 14
		Me.Button6.Text = "NN (F-MLEG)"
		Me.Button6.UseVisualStyleBackColor = True
		Me.Button6.Visible = False
		'
		'Button7
		'
		Me.Button7.Location = New System.Drawing.Point(698, 4)
		Me.Button7.Name = "Button7"
		Me.Button7.Size = New System.Drawing.Size(83, 23)
		Me.Button7.TabIndex = 15
		Me.Button7.Text = "LO (O-MLEG)"
		Me.Button7.UseVisualStyleBackColor = True
		Me.Button7.Visible = False
		'
		'Button8
		'
		Me.Button8.Location = New System.Drawing.Point(444, 41)
		Me.Button8.Name = "Button8"
		Me.Button8.Size = New System.Drawing.Size(75, 23)
		Me.Button8.TabIndex = 16
		Me.Button8.Text = "Void"
		Me.Button8.UseVisualStyleBackColor = True
		Me.Button8.Visible = False
		'
		'Button9
		'
		Me.Button9.Location = New System.Drawing.Point(525, 41)
		Me.Button9.Name = "Button9"
		Me.Button9.Size = New System.Drawing.Size(75, 23)
		Me.Button9.TabIndex = 17
		Me.Button9.Text = "Void"
		Me.Button9.UseVisualStyleBackColor = True
		Me.Button9.Visible = False
		'
		'Button10
		'
		Me.Button10.Location = New System.Drawing.Point(617, 41)
		Me.Button10.Name = "Button10"
		Me.Button10.Size = New System.Drawing.Size(75, 23)
		Me.Button10.TabIndex = 18
		Me.Button10.Text = "Void"
		Me.Button10.UseVisualStyleBackColor = True
		Me.Button10.Visible = False
		'
		'Button11
		'
		Me.Button11.Location = New System.Drawing.Point(698, 41)
		Me.Button11.Name = "Button11"
		Me.Button11.Size = New System.Drawing.Size(75, 23)
		Me.Button11.TabIndex = 19
		Me.Button11.Text = "Void"
		Me.Button11.UseVisualStyleBackColor = True
		Me.Button11.Visible = False
		'
		'Form1
		'
		Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
		Me.ClientSize = New System.Drawing.Size(1063, 601)
		Me.Controls.Add(Me.Button11)
		Me.Controls.Add(Me.Button10)
		Me.Controls.Add(Me.Button9)
		Me.Controls.Add(Me.Button8)
		Me.Controls.Add(Me.Button7)
		Me.Controls.Add(Me.Button6)
		Me.Controls.Add(Me.Button5)
		Me.Controls.Add(Me.Button4)
		Me.Controls.Add(Me.Button3)
		Me.Controls.Add(Me.Button2)
		Me.Controls.Add(Me.wbReply)
		Me.Controls.Add(Me.Button1)
		Me.Controls.Add(Me.wbSent)
		Me.Controls.Add(Me.lbNoProcessed)
		Me.Controls.Add(Me.lbLastProcessed)
		Me.Controls.Add(Me.Label2)
		Me.Controls.Add(Me.Label1)
		Me.Controls.Add(Me.btToggleTimer)
		Me.Controls.Add(Me.Splitter1)
		Me.Name = "Form1"
		Me.Text = "Clearing House Processing"
		CType(Me.QueueTimer, System.ComponentModel.ISupportInitialize).EndInit()
		Me.ResumeLayout(False)

	End Sub

#End Region

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		'RunTest()

		'If System.Diagnostics.Debugger.IsAttached = False Then
		'reloadproducts()
		'End If

		'CheckUnsubmitted()
		QueueTimer.Interval = 10000
		QueueTimer.Start()
		QueueTimer.Enabled = False

		btToggleTimer_Click(Nothing, Nothing)
		Globals.MainForm = Me

		wbSent.Navigate("http://google.com")
		wbReply.Navigate("http://google.com")
		'Exit Sub
	End Sub

	Private Sub RunTest()
		Dim logon As New LogonMsg
		'		Dim terms As New TermsMsg

		Dim sql As String
		Dim dbi As dbInterface = Globals.dbi
		sql = "select nymx_login, nymx_pass from users where UserId=0"

		dbi.query(sql)

		logon.LogonType = UserReqTypes.Logon
		'logon.UserName = dbi.getval("nymx_login")
		logon.Password = dbi.getval("nymx_pass")

		Globals.UserName = dbi.getval("nymx_login")

		If (logon.Submit()) Then

			Dim temp As ClearingQueue = New ClearingQueue()

			temp.RunClearing()
			' after logon, test message
			'Dim tm As New TermsMsg

			'tm.ProductID = "CL"
			'tm.SetReqTyp("FUT")
			'tm.Submit()

			Dim sp As New StrikesMsg

			sp.ProductID = "KD"
			sp.StartTerm = "20120817"
			sp.EndTerm = "20120817"

			'            sp.Submit()

			'Dim tm As New TermsMsg

			'tm.ProductID = "KD"
			'tm.SetReqTyp(Globals.GetSecTyp("KD")) 'Globals.TranslateType(m_firstcfi))
			'tm.Submit()

			'Dim sr As New SpreadReqMsg

			'sr.ProductID = "LO"
			'sr.Submit()

			'Dim tr As New TradersMsg

			'tr.FirmID = "657_morganstanley"
			'tr.ProductID = "CL"
			'tr.Submit()

			'Dim am As New AccountsMsg

			'am.ProductID = "CL"
			'am.SecTyp = "FUT"
			'am.Submit()

			'Need to get underlying product info still
			'
			'Dim sp As New StrikesMsg

			'sp.UnderlyingProduct = "CL"
			'sp.ProductID = "LO"
			'sp.StartTerm = "201208"
			'sp.Submit()

			' now, logoff
			logon.LogonType = UserReqTypes.Logoff

			logon.Submit()
		End If
	End Sub

	Private Sub CheckUnsubmitted()

		Return
		Dim msg As New MailMessage

		msg.To.Add("pallison@stoneguardian.com")

		msg.From = New MailAddress("imperialhoods@stoneguardian.com", "Phil")
		msg.Subject = "testing it out"
		msg.Body = "You have Task List Items which may require Clearing"

		Dim mailer As New System.Net.Mail.SmtpClient


		mailer.Host = "mail.stoneguardian.com"
		mailer.Credentials = New System.Net.NetworkCredential("imperialhoods+stoneguardian.com", "wsihsgc")
		mailer.Send(msg)

	End Sub
	Private Sub reloadproducts(Optional ByVal force As Boolean = False)
		Dim logon As New LogonMsg
		'		Dim terms As New TermsMsg

		' do not run this during business hours unless requested
		If (DateTime.Now.Hour > 8 And DateTime.Now.Hour < 17) And force = False Then
			Return
		End If
		Dim sql As String
		Dim dbi As dbInterface = Globals.dbi
		sql = "select nymx_login, nymx_pass from users where UserId=0"

		dbi.query(sql)

		logon.LogonType = UserReqTypes.Logon
		logon.Password = dbi.getval("nymx_pass")
		Globals.UserName = dbi.getval("nymx_login")

		If logon.Submit() Then
			Dim prods As New ProductsMsg

			prods.Submit()

			'm_clearque.RequestAcctUpdate("CL")
			m_clearque.UpdateAllAccts()

			'clear out and reload firm data/names
			dbi.nonquery("delete from nymx_firm_data")

			Dim pm As New PartiesMsg

			pm.ReqTyp = "7"
			pm.Submit()

			pm.ReqTyp = "1"
			pm.Submit()

			dbi.SetupStoredProc("sp_nymx_acct_xr_name")
			dbi.RunStoredProc()

			logon.LogonType = UserReqTypes.Logoff

			logon.Submit()
		End If

	End Sub

	Private Sub QueueTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles QueueTimer.Elapsed
		' if the timer was already stopped, do not process this event
		Try

			'diable any checks between 11pm and 6am
			If (DateTime.Now.Hour > 23 Or DateTime.Now.Hour < 6) Then
				Return
			End If

			If QueueTimer.Enabled Then

				Dim dbi As dbInterface = Globals.dbi
				dbi.SetupStoredProc("sp_nymx_acct_xr_name")
				dbi.RunStoredProc()

				QueueTimer.Enabled = False
				QueueTimer.Stop()

				checkForDate()

				While m_clearque.CheckForResolvedData()
					m_proced += 1
				End While


				While m_clearque.CheckForWaitingItems()
					m_proced += 1
				End While


				While m_clearque.CheckForNewItems()
					m_proced += 1
				End While

				Me.lbLastProcessed.Text = m_clearque.LastMsg
				Me.lbNoProcessed.Text = m_proced.ToString

				m_clearque.LogoffCPCID()

				QueueTimer.Enabled = True
				QueueTimer.Start()
			End If
		Catch ex As Exception
			Log.LogData(ex.ToString & "\n" & ex.StackTrace)
		End Try

	End Sub

	Private Sub btToggleTimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btToggleTimer.Click
		If QueueTimer.Enabled Then
			QueueTimer.Enabled = False
			btToggleTimer.Text = "Start Timer"
		Else
			QueueTimer.Enabled = True
			btToggleTimer.Text = "Stop Timer"
		End If
	End Sub

	Private Sub checkForDate()
		If Me.m_lastDate = Nothing Then
			m_lastDate = Date.Now
		End If

		If m_lastDate.Day <> Date.Now.Day Then
			m_lastDate = Date.Now
			Me.lbNoProcessed.Text = 0
			m_proced = 0
			Me.reloadproducts()
		End If

	End Sub

	Public Sub SetSent(ByVal inp As String)
		SetWB(inp, wbSent)
	End Sub

	Public Sub SetReply(ByVal inp As String)
		SetWB(inp, wbReply)
	End Sub

	Private Sub SetWB(ByVal inp As String, ByVal wbWin As WebBrowser)
		Dim sxml As XmlDocument = New XmlDocument
		sxml.LoadXml(inp)

		Dim s As String = "defaultss.xsl"
		Dim xr As System.Xml.XmlReader = System.Xml.XmlReader.Create(s)
		Dim xct As Xsl.XslCompiledTransform = New Xsl.XslCompiledTransform()
		xct.Load(xr)

		Dim sb As New StringBuilder()
		Dim xw As XmlWriter = XmlWriter.Create(sb)
		xct.Transform(sxml, xw)
		wbWin.DocumentText = sb.ToString()
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		RunTest()
	End Sub

	Private Sub ChangeClearStatus(ByVal id As String, Optional ByVal status As String = "0")
		Dim dbi As dbInterface = Globals.dbi
		Dim sql As String = "update clearing_trades set clearing_status=" & status & " where clear_id=" & id	'29712"

		dbi.query(sql)

	End Sub
	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		ChangeClearStatus("29093")
		'Dim dbi As dbInterface = Globals.dbi
		'Dim sql As String = "update clearing_trades set clearing_status=0 where clear_id=29093"	'29712"

		'dbi.query(Sql)

	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		reloadproducts(True)
	End Sub

	Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
		ChangeClearStatus("29953")
	End Sub

	Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
		ChangeClearStatus("29952")
	End Sub

	Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
		ChangeClearStatus("29881")
	End Sub

	Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
		ChangeClearStatus("29889")
	End Sub

	Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
		ChangeClearStatus("29953", "8")
	End Sub

	Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
		ChangeClearStatus("29881", "8")
	End Sub

	Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
		ChangeClearStatus("29952", "8")
	End Sub

	Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
		ChangeClearStatus("29889", "8")
	End Sub
End Class
